package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.SupplierDao;
import com.xueyu.domain.SupplierBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supplier")
public class SupplierController{
    @Resource
    SupplierDao supplierDao;

    Map map = new HashMap<>();

    @RequestMapping("/list")
    public Map list(HttpServletRequest req){
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int curPage = Integer.parseInt(req.getParameter("curPage"));
        String search = req.getParameter("search");
        QueryWrapper<SupplierBean> queryWrapper = new QueryWrapper<SupplierBean>();
        Page<SupplierBean> page = new Page<SupplierBean>(curPage,pageSize);
        queryWrapper.like("supplierName",search);
        queryWrapper.or().like("contact",search);
        queryWrapper.or().like("mobile",search);
        queryWrapper.or().like("address",search);
        queryWrapper.or().like("remark",search);
        queryWrapper.or().like("suppliers",search);
        page = supplierDao.selectPage(page,(search == null || search.equals(""))? null : queryWrapper);
        List<SupplierBean> list = page.getRecords();
        map.put("list",list);
        map.put("code",200);
        map.put("total",page.getTotal());
        return map;
    }

    @RequestMapping("/insert")
    public Map insert(SupplierBean supplierBean){
        map.put("addChanged",supplierDao.insert(supplierBean));
        return map;
    }

    @RequestMapping("/update")
    public Map update(SupplierBean supplierBean){
        map.put("changed",supplierDao.updateById(supplierBean));
        return map;
    }

    @RequestMapping("/del/{id}")
    public Map del(@PathVariable("id") int id){
        map.put("delChanged",supplierDao.deleteById(id));
        return map;
    }
}
